#include "threadPool.h"
#include <errno.h>
char* removeLastChar(const char* pathname){
    //回退掉最后/之后的内容
    int len = strlen(pathname);
    char* ret = (char*)malloc(1024);
    strcpy(ret,pathname);
    for(int i = len-1;i>=0;i--){
        if(ret[i] != '/'){
            ret[i] = '\0';
            break;
        }
    }

    return ret;
}
bool judge(const char* userpath,const char * pathname){
    int  userLen = strlen(userpath);
    for(int i = 0;i<userLen;i++){
        if(userpath[i] != pathname[i]){
            return false;
        }
    }
    return true;
}

int netDriveMkdir(const char *pathname,const char *dirname,int netfd){
    char path[1024];
    int pathLen = strlen(pathname);

    if(dirname[0] == '/'){
        strcpy(path,dirname);          
    }else{
        sprintf(path,"%s/%s",pathname,dirname);
    }
    DIR* dir;
    dir = opendir(path);
    if(dir == NULL  && errno == ENOENT){
        int ret = mkdir(path,0777);
        if(ret == 0){
            char tmp[10] = "SUCCESS";
            //int len = 8;
            //write(netfd,&len,sizeof(int));
            //write(netfd,tmp,len);
            train_t train;
            train.len=strlen(tmp);
            memcpy(train.data,tmp,train.len);
            send(netfd,&train,sizeof(int)+train.len,MSG_NOSIGNAL);
        }else{
            char tmp[10] = "FAIL";
            int len = 5;
            write(netfd,&len,sizeof(int));
            write(netfd,tmp,len);
            //有相同的文件  
        }
        return 1;   
    }else{
        char ret[10] = "FAIL";
        int len = 5;
        write(netfd,&len,sizeof(int));
        write(netfd,ret,len);

        //有相同的文件
        return -1;
    } 
}

int netDriveRmdir(const char *pathname,const char *dirname,int netfd){
    char path[1024];
    int pathLen = strlen(pathname);

    if(dirname[0] == '/'){
        strcpy(path,dirname);          
    }else{
        sprintf(path,"%s/%s",pathname,dirname);
    }
    DIR* dir;
    dir = opendir(path);
    if(dir == NULL  && errno == ENOENT){
        char ret[10] = "FAIL";
        int len = 5;
        write(netfd,&len,sizeof(int));
        write(netfd,ret,len);
        //有相同的文件
        return -1;
    }else{

        int ret = rmdir(path);
        if(ret == 0){
            char tmp[10] = "SUCCESS";
            //int len = strlen(tmp);
            //write(netfd,&len,sizeof(int));
            //write(netfd,tmp,len);
            train_t train;
            train.len=strlen(tmp);
            memcpy(train.data,tmp,train.len);
            send(netfd,&train,sizeof(int)+train.len,MSG_NOSIGNAL);
            return 1;
        }else{
           char tmp[10] = "FAIL";
            int len = strlen(tmp);
            write(netfd,&len,sizeof(int));
            write(netfd,tmp,len);
            return -1;
        }

        return 1;   
    } 
}


int netDriveRemove(const char* pathname,const char* filename,int netfd){
    char buf[1024]; 
    if(filename == NULL){
        char ret[10] = "FAIL";
        int len = 5;
        write(netfd,&len,sizeof(int));
        write(netfd,ret,len);
        return -1;
    }
    if(filename[0] == '/'){
        strcpy(buf,pathname);
    }else{
        sprintf(buf,"%s/%s",pathname,filename);
    }
    printf("buf=%s\n",buf);
    int ret = unlink(buf);

    if(ret == -1){
        char ret[10] = "FAIL";
        int len = 5;
        write(netfd,&len,sizeof(int));
        write(netfd,ret,len);
        return -1;
    }else{
        char ret[10] = "SUCCESS";
        int len = 8;
        write(netfd,&len,sizeof(int));
        write(netfd,ret,len);
        return 1;
    }
    return 1;
}



